package com.lufei.message_system.Controller;

import com.lufei.message_system.Data.PageResult;
import com.lufei.message_system.Data.QuestionVD.QuestionBatchCreateDTO;
import com.lufei.message_system.Data.QuestionVD.QuestionCreateDTO;
import com.lufei.message_system.Data.QuestionVD.QuestionUpdateDTO;
import com.lufei.message_system.Data.QuestionVD.QuestionVO;
import com.lufei.message_system.Data.Result;
import com.lufei.message_system.Service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 创建问题
    @PostMapping
    public Result createQuestion(@RequestBody @Valid QuestionCreateDTO createDTO) {

        return questionService.createQuestion(createDTO);
    }

    //批量创建数据
    @PostMapping("/batch")
    public Result batchCreateQuestions(@RequestBody @Valid QuestionBatchCreateDTO batchDTO) {
        return questionService.batchCreateQuestions(batchDTO.getQuestions());
    }

    // 获取问题详情
    @GetMapping("/{id}")
    public Result<QuestionVO> getQuestion(@PathVariable Long id) {
        return questionService.getQuestion(id);
    }

    // 更新问题
    @PutMapping("/{id}")
    public Result updateQuestion(@PathVariable Long id,
                                 @RequestBody @Valid QuestionUpdateDTO updateDTO) {
        return questionService.updateQuestion(id, updateDTO);
    }

    // 删除问题
    @DeleteMapping("/{id}")
    public Result deleteQuestion(@PathVariable Long id) {
        return questionService.deleteQuestion(id);
    }

    // 分页查询问题
    @GetMapping
    public Result<PageResult<QuestionVO>> listQuestions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String dimension) {
        System.out.println("接收到查询问题请求: page=" + page + ", size=" + size + 
                          ", content=" + content + ", dimension=" + dimension);
        Result<PageResult<QuestionVO>> result = questionService.listQuestions(page, size, content, dimension);
        
        // 打印查询结果中的题目分数
        if (result.getCode() == 200 && result.getData() != null && result.getData().getList() != null) {
            System.out.println("查询到 " + result.getData().getList().size() + " 条题目数据");
            for (QuestionVO question : result.getData().getList()) {
                System.out.println("题目ID=" + question.getId() + ", 维度=" + question.getDimension() + 
                                 ", 内容=" + question.getContent());
                System.out.println("选项A=" + question.getOptionA() + ", 分数: " +
                                 "aEScore=" + question.getAEScore() + 
                                 ", aIScore=" + question.getAIScore() +
                                 ", aSScore=" + question.getASScore() +
                                 ", aNScore=" + question.getANScore() +
                                 ", aTScore=" + question.getATScore() +
                                 ", aFScore=" + question.getAFScore() +
                                 ", aJScore=" + question.getAJScore() +
                                 ", aPScore=" + question.getAPScore());
                System.out.println("选项B=" + question.getOptionB() + ", 分数: " +
                                 "bEScore=" + question.getBEScore() + 
                                 ", bIScore=" + question.getBIScore() +
                                 ", bSScore=" + question.getBSScore() +
                                 ", bNScore=" + question.getBNScore() +
                                 ", bTScore=" + question.getBTScore() +
                                 ", bFScore=" + question.getBFScore() +
                                 ", bJScore=" + question.getBJScore() +
                                 ", bPScore=" + question.getBPScore());
                System.out.println("-----------------------------------");
            }
        }
        
        return result;
    }

    // 获取所有测试题目（不分页）
    @GetMapping("/all")
    public Result<List<QuestionVO>> getAllQuestions() {
        return questionService.getAllQuestions();
    }
}